package com.bwie.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bwie.user.entity.GoodsEntity;
import com.bwie.user.service.GoodsService;
import com.bwie.user.vo.ActivityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.user.entity.ActivityEntity;
import com.bwie.user.service.ActivityService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author ShuangzhiZhao
 * @email 466505908@qq.com
 * @date 2022-02-22 11:01:46
 */
@RestController
@RequestMapping("user/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = activityService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 秒杀活动列表
     */
    @RequestMapping("/mslist")
    public R mslist(){
        List<ActivityVo> page=activityService.mslist();
        return R.ok().put("page", page);
    }

    /**
     * 回显vo里的信息（商品基本信息，秒杀商品的信息）
     */
    @RequestMapping("/info/{aid}")
    public R info(@PathVariable("aid") Long aid){
		ActivityEntity activity = activityService.getById(aid);
		if (activity!=null){
            GoodsEntity goods = goodsService.getById(activity.getGoodsId());
            return R.ok().setData(activity).put("activity",activity).put("goods",goods);
        }

        return R.error();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ActivityEntity activity){
		activityService.save(activity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ActivityEntity activity){
        boolean b = activityService.updateById(activity);

        return R.ok().put("b",b);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] aids){
		activityService.removeByIds(Arrays.asList(aids));

        return R.ok();
    }

}
